package com.unique.flyme.match.netServiceTools.meizuApi;

import android.app.Activity;
import android.util.Log;

import com.unique.flyme.match.config.Config;

import org.json.JSONException;
import org.json.JSONObject;

import sdk.meizu.auth.MzAuthenticator;
import sdk.meizu.auth.OAuthError;
import sdk.meizu.auth.OAuthToken;
import sdk.meizu.auth.callback.ImplictCallback;

/**
 * Description:
 * User: Dream_Coder(chenchen_839@126.com)
 * Date: 2015-10-24
 * Time: 22:57
 */
public class ApiUtils {

    public static final String GET_USER_INFO = "https://open-api.flyme.cn/v2/me";
    public String TAG = ApiUtils.this.getClass().getSimpleName();
    private static MzAuthenticator mAuthenticator = null;
    private String authCode = null;
    private String accessToken = null;
    private static Activity mActivity = null;
    public static ApiUtils apiObject = null;

    private OnFailedLoginListener failedListner = null;
    private OnSuccessLoginListener successListener = null;

    public interface OnSuccessLoginListener{
        void onSuccessHandler(JSONObject response);
    }
    public interface OnFailedLoginListener{
        void onFailedHandler(String msg);
    }
    private ApiUtils() {
    }

    //单例生成api操作对象
    public static ApiUtils getInstance(Activity context) {
        if (apiObject == null) {
            apiObject = new ApiUtils();
        }
        mActivity = context;
        return apiObject;
    }

    /**
     * 第一步:
     * 获取ACCESS_TOKEN
	 * 采用ImplictAuth方式认证
     */
    public void getAuthorization(final OnFailedLoginListener failedListener, final OnSuccessLoginListener successListener) {
        this.failedListner = failedListener;
        this.successListener = successListener;
        if (mAuthenticator == null) {
            mAuthenticator = new MzAuthenticator(Config.CLIENT_ID, Config.REDIRECT_URL);
        }
        mAuthenticator.requestImplictAuth(mActivity, Config.SCOPE, new ImplictCallback() {
            @Override
            public void onError(OAuthError oAuthError) {
                failedListener.onFailedHandler(oAuthError.getError());
                Log.i("ApiUtils","错误"+1);
            }

            @Override
            public void onGetToken(OAuthToken oAuthToken) {
                accessToken = oAuthToken.getAccessToken();
                getUserInfo(failedListener,successListener);
            }
        });
     
    }

    /**
     * 获取用户信息
     * @param failedListener
     * @param successListener
     */
    private void getUserInfo(final OnFailedLoginListener failedListener, final OnSuccessLoginListener successListener) {
        JSONObject object = new JSONObject();
        JsonSendMeizuHelper jsonSendMeizuHelper = new JsonSendMeizuHelper(mActivity);
        try {
            object.put("access_token",accessToken);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        jsonSendMeizuHelper.getGetJsonObjectResponse(GET_USER_INFO, object, new OnSuccessLoginListener() {
            @Override
            public void onSuccessHandler(JSONObject response) {
                successListener.onSuccessHandler(response);
            }
        }, new OnFailedLoginListener() {
            @Override
            public void onFailedHandler(String msg) {
                failedListener.onFailedHandler(msg);
            }
        });

    }
}
